package com.tywh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tywh.dao.CtsDao;
import com.tywh.pojo.CstBill;
import com.tywh.pojo.PageBean;
import com.tywh.pojo.ResponseData;
import com.tywh.service.CtsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xhx
 * @date 2018年4月19日 下午3:26:40
 */
@Service
public class CtsServiceImpl implements CtsService {
	
	@Autowired
	private CtsDao ctsDao;

	@Override
	public ResponseData GetCtsBill(PageBean pageBean) {
		ResponseData responseData = new ResponseData();
		if(null == pageBean.getCSTCODE() || "".equals(pageBean.getCSTCODE())){
			responseData.setStatusCode("-1");
			responseData.setHttpCode("200");
			responseData.setContent("供应商编码不能为空");
			return responseData;
		}
		List<CstBill> ctsBillList = null;
		if(null == pageBean.getPAGENUM() || "".equals(pageBean.getPAGENUM())){
			pageBean.setPAGENUM("1");
		}
		if(Integer.valueOf(pageBean.getPAGENUM()) < 1){
			pageBean.setPAGENUM("1");
		}
		if(null == pageBean.getPAGESIZE() || "".equals(pageBean.getPAGESIZE())){
			pageBean.setPAGESIZE("100");
		}
		if(Integer.valueOf(pageBean.getPAGESIZE()) < 1 || Integer.valueOf(pageBean.getPAGESIZE()) > 1000){
			pageBean.setPAGESIZE("100");
		}
		if(null == pageBean.getSTARTTIME() || "".equals(pageBean.getSTARTTIME()) ||
				null == pageBean.getENDTIME() || "".equals(pageBean.getENDTIME())){
			//当前时间
			LocalDateTime localDateTime = LocalDateTime.now();
			String time = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//			System.out.println("当前时间：" + time);
			pageBean.setENDTIME(time);
			//一个月前时间
			LocalDateTime localDateTimeMonth = localDateTime.minus(1, ChronoUnit.MONTHS);
			String timeMonth = localDateTimeMonth.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//			System.out.println(timeMonth);
			pageBean.setSTARTTIME(timeMonth);
		}else{
			LocalDateTime localDateTimeStart = null;
			LocalDateTime localDateTimeEnd = null;
			try {
//				System.out.println(pageBean.getSTARTTIME() + "end" + pageBean.getENDTIME());
				localDateTimeStart = LocalDateTime.parse(pageBean.getSTARTTIME(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
				localDateTimeEnd = LocalDateTime.parse(pageBean.getENDTIME(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
			}catch (DateTimeParseException dtpe){
				dtpe.getStackTrace();
				responseData.setStatusCode("-1");
				responseData.setHttpCode("200");
				responseData.setContent("日期正确格式为:yyyy-MM-dd HH:mm:ss " + dtpe.getMessage());
				return responseData;
			}
			//开始时间在结束时间之后
			if(localDateTimeStart.isAfter(localDateTimeEnd)){
				responseData.setStatusCode("-1");
				responseData.setHttpCode("200");
				responseData.setContent("开始时间应该小于结束时间");
				return responseData;
			}
			//结束时间在当前时间之后
			if(localDateTimeEnd.isAfter(LocalDateTime.now())){
				responseData.setStatusCode("-1");
				responseData.setHttpCode("200");
				responseData.setContent("结束时间应该小于当前时间");
				return responseData;
			}
		}

		try {
			ctsBillList = ctsDao.GetCtsBill(pageBean);
		}catch (Exception e){
			e.printStackTrace();
			responseData.setStatusCode("-1");
			responseData.setHttpCode("200");
			responseData.setContent(e.getMessage());
			return responseData;
		}
		responseData.setStatusCode("1");
		responseData.setHttpCode("200");
		responseData.setContent("请求成功");
		responseData.setData(ctsBillList);
		return responseData;
	}

	@Override
	public ResponseData GetCtsBillPage(PageBean pageBean) {
		ResponseData responseData = new ResponseData();
		if(null == pageBean.getCSTCODE() || "".equals(pageBean.getCSTCODE())){
			responseData.setStatusCode("-1");
			responseData.setHttpCode("200");
			responseData.setContent("供应商编码不能为空");
			return responseData;
		}
		if(null == pageBean.getPAGENUM() || "".equals(pageBean.getPAGENUM())){
			pageBean.setPAGENUM("1");
		}
		if(Integer.valueOf(pageBean.getPAGENUM()) < 1){
			pageBean.setPAGENUM("1");
		}
		if(null == pageBean.getPAGESIZE() || "".equals(pageBean.getPAGESIZE())){
			pageBean.setPAGESIZE("100");
		}
		if(Integer.valueOf(pageBean.getPAGESIZE()) < 1 || Integer.valueOf(pageBean.getPAGESIZE()) > 1000){
			pageBean.setPAGESIZE("100");
		}
		//起始与结束时间都为空，则系统生成近一年的数据
		if((null == pageBean.getSTARTTIME() || "".equals(pageBean.getSTARTTIME())) &&
				(null == pageBean.getENDTIME() || "".equals(pageBean.getENDTIME()))){
			//当前时间
			LocalDateTime localDateTime = LocalDateTime.now();
			String time = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//			System.out.println("当前时间：" + time);
			pageBean.setENDTIME(time);
			//一年前时间
			LocalDateTime localDateTimeMonth = localDateTime.minus(1, ChronoUnit.YEARS);
			String timeMonth = localDateTimeMonth.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//			System.out.println(timeMonth);
			pageBean.setSTARTTIME(timeMonth);
		//起始与结束时间都有，判断传入日期是否规范
		}else if(null != pageBean.getSTARTTIME() && !"".equals(pageBean.getSTARTTIME()) &&
				null != pageBean.getENDTIME() && !"".equals(pageBean.getENDTIME())){
			LocalDateTime localDateTimeStart = null;
			LocalDateTime localDateTimeEnd = null;
			try {
//				System.out.println(pageBean.getSTARTTIME() + "end" + pageBean.getENDTIME());
				localDateTimeStart = LocalDateTime.parse(pageBean.getSTARTTIME(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
				localDateTimeEnd = LocalDateTime.parse(pageBean.getENDTIME(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
			}catch (DateTimeParseException dtpe){
				dtpe.getStackTrace();
				responseData.setStatusCode("-1");
				responseData.setHttpCode("200");
				responseData.setContent("日期正确格式为:yyyy-MM-dd HH:mm:ss " + dtpe.getMessage());
				return responseData;
			}
			//开始时间在结束时间之后
			if(localDateTimeStart.isAfter(localDateTimeEnd)){
				responseData.setStatusCode("-1");
				responseData.setHttpCode("200");
				responseData.setContent("开始时间应该小于结束时间");
				return responseData;
			}
			//结束时间在当前时间之后
			if(localDateTimeEnd.isAfter(LocalDateTime.now())){
				responseData.setStatusCode("-1");
				responseData.setHttpCode("200");
				responseData.setContent("结束时间应该小于当前时间");
				return responseData;
			}
		}else{
			//只传入了一个时间
			if(null != pageBean.getSTARTTIME() && !"".equals(pageBean.getSTARTTIME())){
				LocalDateTime localDateTimeStart = LocalDateTime.parse(pageBean.getSTARTTIME(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
				if(localDateTimeStart.isAfter(LocalDateTime.now())){
					responseData.setStatusCode("-1");
					responseData.setHttpCode("200");
					responseData.setContent("开始时间应该小于当前时间");
					return responseData;
				}
			}else{
				LocalDateTime localDateTimeEnd = LocalDateTime.parse(pageBean.getENDTIME(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
				if(localDateTimeEnd.isAfter(LocalDateTime.now())){
					responseData.setStatusCode("-1");
					responseData.setHttpCode("200");
					responseData.setContent("结束时间应该小于当前时间");
					return responseData;
				}
			}
		}
		List<CstBill> ctsBillList = null;
		PageInfo<CstBill> pageInfo = null;
		try {
			PageHelper.startPage(Integer.valueOf(pageBean.getPAGENUM()),Integer.valueOf(pageBean.getPAGESIZE()));
			Map<String, Object> map = new HashMap<String,Object>();
			map.put("CSTCODE", pageBean.getCSTCODE());
			map.put("STARTTIME", pageBean.getSTARTTIME());
			map.put("ENDTIME", pageBean.getENDTIME());
			ctsBillList = ctsDao.GetCtsBillPage(map);
			pageInfo = new PageInfo<CstBill>(ctsBillList);
		}catch (Exception e){
			e.printStackTrace();
			responseData.setStatusCode("-1");
			responseData.setHttpCode("200");
			responseData.setContent(e.getMessage());
			return responseData;
		}
		responseData.setStatusCode("1");
		responseData.setHttpCode("200");
		responseData.setContent("请求成功");
		responseData.setData(pageInfo);
		return responseData;
	}
}
